<?php defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH."libraries/MX/Controller.php";

class MY_Controller extends MX_Controller
{
	public $lang_current = 'vietnamese';
	/**
	 * The name of the module that this controller instance actually belongs to.
	 *
	 * @var string 
	 */
	public $module;

	/**
	 * The name of the controller class for the current class instance.
	 *
	 * @var string
	 */
	public $controller;

	/**
	 * The name of the method for the current request.
	 *
	 * @var string 
	 */
	public $method;

	/**
	 * Load and set data for some common used libraries.
	 */
	public $lang_array;
	public $lang_array_current;
	public $_lang;
	protected $frefix_child = '►';
	public $year_start = 2013; // năm bắt đầu sử dụng app
	public function __construct()
	{
		parent::__construct();
		//load ngon ngu
		$this->load->config('language');
		$this->lang_array = $this->config->item('supported_languages');
		$l = $this->session->userdata('ss_lang');
		
		$this->lang_array_current = $l ? $l : $this->lang_array[$this->config->item('default_language')];
		
		$this->_lang = $this->lang_array_current['id'];
		$this->lang_current = $this->lang_array_current['folder'];
		$this->session->set_userdata('ss_lang',$this->lang_array_current);
		
		$this->benchmark->mark('my_controller_start');
		
		$this->template->add_theme_location(APPPATH.'themes/');//ADDONPATH
		// Migration logic helps to make sure PyroCMS is running the latest changes
		$this->load->library('migration');
		
		if ( ! ($schema_version = $this->migration->current()))
		{
			show_error($this->migration->error_string());
		}

		// Result of schema version migration
		elseif (is_numeric($schema_version))
		{
			log_message('debug', 'PyroCMS was migrated to version: ' . $schema_version);
		}
		// With that done, load settings
		$this->load->library(array('session'));//'settings/settings'

		// Use this to define hooks with a nicer syntax
		ci()->hooks =& $GLOBALS['EXT'];

		$user = $this->session->userdata('current_user');
		$this->current_user = $user ? $user : NULL;
		$this->current_profile = $this->session->userdata('current_profile');
		$this->background = $this->session->userdata('background');
		$this->template->current_user = ci()->current_user = $this->current_user;

		ci()->module = $this->module = $this->router->fetch_module();
		ci()->controller = $this->controller = $this->router->fetch_class();
		ci()->method = $this->method = $this->router->fetch_method();

		$this->load->model(array(
			'permissions/permission_m',
		));

		// List available module permissions for this user
		ci()->permissions = $this->permissions = $this->current_user ? $this->permission_m->get_group($this->current_user->group_id) : array();
		//print_r(ci()->permissions);exit;

		// load all modules (the Events library uses them all) and make their details widely available
		//ci()->enabled_modules = $this->module_m->get_all();
		ci()->enabled_modules = true;

		// now that we have a list of enabled modules
		//$this->load->library('events');

		// set defaults
		$this->template->module_details = ci()->module_details = $this->module_details = false;
		
		// If the module is disabled, then show a 404.
		//empty($this->module_details['enabled']) AND show_404();

		if ( ! $this->module_details['skip_xss'])
		{
			$_POST = $this->security->xss_clean($_POST);
		}
		//$this->module_details['path'] = $this->template->get_theme_path();
		//echo $this->template->get_theme_path();
		//exit;
		//echo $this->module_details;exit;
		//$this->template->module_details = ci()->module_details = $this->module_details = false;
		if ($this->module )
		{
			$path_module = APPPATH.'modules/'.$this->module;
			//echo $this->module_details['path'];exit;
			Asset::add_path('module', $path_module.'/');
			Asset::set_path('module');
		}

//		$this->load->vars($pyro);
		
		$this->benchmark->mark('my_controller_end');
		
		// Enable profiler on local box
	    if ((isset($this->current_user->group) and $this->current_user->group === 'admin') and is_array($_GET) and array_key_exists('_debug', $_GET))
	    {
			unset($_GET['_debug']);
	    	$this->output->enable_profiler(true);
	    }
		$this->template->set('lang',$this->_lang);
	}
}

/**
 * Returns the CodeIgniter object.
 *
 * Example: ci()->db->get('table');
 *
 * @return \CI_Controller
 */
function ci()
{
	return get_instance();
}
